import numpy as np
from _csdn_data import input_data, weights_data


def my_conv(input, weights):
    h, w = input.shape
    k, _ = weights.shape
    padding = k // 2
    padded = np.zeros((h + padding * 2, w + padding * 2), dtype=np.float32)
    padded[padding:h+1, padding:w+1] = input
    result = np.zeros((h, w), dtype=np.float32)
    for row in range(padding, h + 1):
        for col in range(padding, w + 1):
            window = padded[row - padding:row + padding + 1, col - padding: col + padding + 1]
            result[row - padding, col - padding] = (window * weights).sum()
    return result


def my_depthwise(input, weights):
    c, h, w = input.shape
    c2, k, _ = weights.shape
    result = np.zeros_like(input)
    for ci in range(c):
        result[ci] = my_conv(input[ci], weights[ci])
    return result


# shape=[c,h,w]
input = np.asarray(input_data, np.float32)
# shape=[in_c,k,k]
weights = np.asarray(weights_data, np.float32)
rs = my_depthwise(input, weights)
print(rs)